package com.hunt.controller;

import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.app.StockAllMessage;
import com.hunt.model.app.TransactionCalendar;
import com.hunt.service.FinancialDataServiceNew;
import com.hunt.service.StockAllMessageService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;

/**
 * @Author xys
 * @Date 2021/10/12
 * @Description
 */
@Api(value = "返回股票全部数据")
@Controller
@RequestMapping(path = "/app/stockallmessage")
public class stockallmessageController extends BaseController{
    @Autowired
    private StockAllMessageService stockAllMessageService;
    @Autowired
    private TransactionCalendarMapper transactionCalendarMapper;
    @ApiOperation(value = "获得股票全部数据", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getstockallmessage", method = RequestMethod.POST)
    public Result getstockallmessage(@RequestParam(value = "code", required = true) String code) throws ParseException {
        if(isemptytime()&&istransaction()){
            return Result.success(new StockAllMessage());
        }
        else {
            StockAllMessage stockAllMessage =stockAllMessageService.getStockallmessage(code,istransaction());
            return Result.success(stockAllMessage);
        }

    }
    private  boolean isemptytime(){
        LocalTime now = LocalTime.now();

        return now.isAfter(LocalTime.of(9, 0, 0)) && now.isBefore(LocalTime.of(9, 14, 0));
    }
    private  boolean istransaction(){
        Date date=new Date();
        SimpleDateFormat s=new SimpleDateFormat("yyyyMMdd");
        TransactionCalendar transactionCalendar=transactionCalendarMapper.findByCalDate(s.format(date));
        return transactionCalendar.getIs_open().equals("1");
    }
}